<%#
 Copyright 2018 Lienol <lienol@qq.com>
 Licensed to the public under the Apache License 2.0.
-%>

<%
function trim(s)
  return (s:gsub("^%s*(.-)%s*$", "%1"))
end
local dsp = require "luci.dispatcher"
local gfwlist_version=luci.sys.exec("uci get passwall.@global[0].gfwlist_version")
local chnroute_version=luci.sys.exec("uci get passwall.@global[0].chnroute_version")
local Pcap_Routing_version=luci.sys.exec("uci get passwall.@global[0].pcap_Routing_version")
local Pcap_WhiteList_version=luci.sys.exec("uci get passwall.@global[0].pcap_WhiteList_version")

local gfwlist_update = trim(luci.sys.exec("uci get passwall.@global[0].gfwlist_update"))=="1" and "checked='checked'" or ""
local chnroute_update = trim(luci.sys.exec("uci get passwall.@global[0].chnroute_update"))=="1" and "checked='checked'" or ""
local Pcap_Routing_update = trim(luci.sys.exec("uci get passwall.@global[0].Pcap_Routing_update"))=="1" and "checked='checked'" or ""
local Pcap_WhiteList_update = trim(luci.sys.exec("uci get passwall.@global[0].Pcap_WhiteList_update"))=="1" and "checked='checked'" or ""
-%>

<style>
#_ss_rule_fieldset {
	margin-bottom: 0px;
}
#_ss_rule_table {
	margin-bottom: 10px;
}
#_ss_rule_table td {
	border:none;
}
#_ss_rule_table tr td:first-child {
	width: 20%;
	text-align:right;
}
#_ss_rule_table tr td+td {
}
</style>
<script type="text/javascript">//<![CDATA[
	function update_rules(btn)
	{
		btn.disabled = true;
		btn.value = '<%:Update...%>';
		var table = document.getElementById('_ss_rule_table');
		var domList = table.getElementsByTagName('input');
		var checkBoxList = [];
		var len = domList.length;
		while (len--) {
			var dom=domList[len];
　　		if (dom.type == 'checkbox' && dom.checked) {
  　　			checkBoxList.push(dom.name);
　　		}
		}
		XHR.get('<%=dsp.build_url("admin/vpn/passwall/update_rules")%>',{update:checkBoxList},
			function(x,data)
			{
				if (x && x.status == 200) {
					alert(data.msg);
					window.setTimeout(function () {
						window.location.href='<%=dsp.build_url("admin/vpn/passwall/log")%>';
					}, 1000);
				}
				else {
					alert("接口错误！");
					btn.disabled = false;
					btn.value = '手动更新';
				}
			}
		);
		return false;
	}
//]]></script>
<fieldset id="_ss_rule_fieldset" class="cbi-section">
	<table width="100%" cellspacing="10" id="_ss_rule_table">
		<tr>
			<td>gfwlist版本</td>
			<td>
				<span>【 <%=gfwlist_version%>】</span>
				<input type="checkbox" name="gfwlist_update" value="1" <%=gfwlist_update%> />
			</td>
		</tr>
		<tr>
			<td>chnroute版本</td>
			<td>
				<span>【 <%=chnroute_version%>】</span>
				<input type="checkbox" name="chnroute_update" value="1" <%=chnroute_update%> />
			</td>
		</tr>
		<tr>
			<td>Pcap_Routing版本</td>
			<td>
				<span>【 <%=Pcap_Routing_version%>】</span>
				<input type="checkbox" name="Pcap_Routing_update" value="1" <%=Pcap_Routing_update%> />
			</td>
		</tr>
		<tr>
			<td>Pcap_WhiteList版本</td>
			<td>
				<span>【 <%=Pcap_WhiteList_version%>】</span>
				<input type="checkbox" name="Pcap_WhiteList_update" value="1" <%=Pcap_WhiteList_update%> />
			</td>
		</tr>
		<tr>
			<td style="padding-top: 15px;">更新规则</td>
			<td>
				<input class="cbi-button cbi-input-apply" type="submit" id="update_rules_btn" onclick="update_rules(this)" value="手动更新" >
			</td>
		</tr>
	</table>
</fieldset>